var $ = require('jquery');
var imagePicker = require('./../../imagePicker');
var chatPeer = require('./../../chatPeer');
var html = require('./index.html');
var $toolbarTmp = $(html);

/**
 * 创建toolbar, 并绑定事件
 * @returns {*}
 */
module.exports.create = function ($parent) {
  var $toolbar = $toolbarTmp.clone();
  var $uploadBtn = $toolbar.find('.upload-btn');
  imagePicker.bind($uploadBtn, function (url) {
    insertImageAndUpload($uploadBtn, url);
  });
  $parent.append($toolbar);
  return $toolbar;
};

/**
 * 将图片插入到编辑器中并上传
 * @param $uploadBtn
 * @param url
 */
function insertImageAndUpload($uploadBtn, url) {
  var $img = $('<img>');
  $img.css({
    'max-width': '120px',
    height: 'auto'
  });
  $img.attr('src', url);
  // $img.attr('id', id);
  $img.attr('contenteditable', false);
  $img.attr('oncontrolselect', 'return false;');
  $uploadBtn.closest('.im-edit-area').find('.ipt-content').append($img);
}
